package com.example.androidframework.ui.privacy

import android.content.Context
import android.graphics.Color
import android.text.Html
import android.text.Spannable
import android.text.SpannableString
import android.text.SpannableStringBuilder
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.view.View
import android.webkit.WebView
import android.widget.TextView
import com.example.androidframework.R
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.CenterPopupView
import com.martin.lib_base.ktx_live.buildRequest
import com.martin.lib_base.ktx_live.launchCollect
import com.martin.lib_base.net.NetApply


class FirstPrivacyPopView(
    context: Context,
    var type: Int = 1,
    var onCanel: () -> Unit,
    var onSubmit: () -> Unit,
) : CenterPopupView(context) {

    var webView: WebView? = null

    override fun getImplLayoutId(): Int {
        return R.layout.pop_view_first_privacy
    }

    override fun onCreate() {
        super.onCreate()

        findViewById<TextView>(R.id.title).text = "用户协议及隐私政策"

        findViewById<TextView>(R.id.submit).text = "同意"

        findViewById<TextView>(R.id.hint).visibility = View.INVISIBLE

        val contentTv = findViewById<TextView>(R.id.content)

        webView = findViewById<WebView>(R.id.content_tv)
        val settings = webView?.settings
        settings!!.javaScriptEnabled = true

        findViewById<TextView>(R.id.cancel).setOnClickListener {
            onCanel.invoke()
            dismiss()
        }

        findViewById<TextView>(R.id.submit).setOnClickListener {
            onSubmit.invoke()
            dismiss()
        }


        val str1 = SpannableString(
            "感谢您信任并使用[弘原坐标]!\n" +
                    "     我们非常重视您的个人信息和隐私保护。为了更好的保障您的个人权益，在您使用找们的产品前，请您认真阅读"
        )
        val str2 = SpannableString("《用户协议》")
        str2.setSpan(object : ClickableSpan() {
            override fun onClick(widget: View) {
                XPopup.Builder(context)
                    .asCustom(UserPrivacyDetailPopView(context, 1, "用户协议", onSubmit = {

                    }, onCanel = {})).show()
            }

            override fun updateDrawState(ds: TextPaint) {
                //super.updateDrawState(ds)
                ds.color = Color.parseColor("#EF8201")
                ds.isUnderlineText = true
            }
        }, 0, str2.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)

        val str22 = SpannableString("和")

        val str3 = SpannableString("《隐私政策》")
        str3.setSpan(object : ClickableSpan() {
            override fun onClick(widget: View) {
                XPopup.Builder(context)
                    .asCustom(UserPrivacyDetailPopView(context, 2, "隐私政策", onSubmit = {

                    }, onCanel = {})).show()
            }

            override fun updateDrawState(ds: TextPaint) {
                // super.updateDrawState(ds)
                ds.color = Color.parseColor("#EF8201")
                ds.isUnderlineText = true
            }
        }, 0, str3.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)

        val str4 =
            SpannableString("的全部内容，同意并接受全部条款后开始使用我们的产品和服务。若选择不同意，将无法使用我们的产品和服务，并会退出应用。")

        val spann = SpannableStringBuilder()
        spann.append(str1)
        spann.append(str2)
        spann.append(str22)
        spann.append(str3)
        spann.append(str4)
        contentTv.movementMethod = LinkMovementMethod.getInstance()
        contentTv.highlightColor = Color.TRANSPARENT
        contentTv.text = spann

        // getContent()

    }

    override fun getMaxWidth(): Int {
        return resources.displayMetrics.widthPixels - 140
    }

    fun getContent() {

        launchCollect({
            NetApply.api.configArticleDetails(
                buildRequest(
                    "Config.ArticleDetails",
                    mapOf(
                        "article_id" to 1
                    )
                )
            )
        }) {
            onSuccess = {
                it.info?.let { it1 ->
                    it1.postContent.let { it2 ->
                        webView?.loadData(Html.fromHtml(it2).toString(), "text/html", "utf-8")
                    }
                }
            }
        }
    }

}